package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.utils.BaseAction;

import com.opensymphony.xwork2.ActionContext;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> 
		 {
	@Autowired
	private StandardService standardService;
	// 保存收派标准的方法
	@Action(value = "standard_save", results = { @Result(name = "success", location = "/pages/base/standard.html", type = "redirect") })
	public String save() {
		standardService.save(model);
		return SUCCESS;
	}

	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	// 实现收派标准的分页查询
	@Action(value = "standard_findAllByPage", results = { @Result(name = "success", type = "json") })
	public String findAllByPage() {
		// 构建pageable的子实现类,PageRequest
		Pageable pageable = new PageRequest(page - 1, rows);
		// 实现分页查询需要pageable,将构建好的穿进去
		Page<Standard> page = standardService.findAllByPage(pageable);
		// 接收到Page对象,需要自己构建total和rows两个属性
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		// 将map转换为json数据返回 ，使用struts2-json-plugin 插件
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

	// 实现快递员增加功能的收派标准下拉框数据查询的方法
	@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
	public String findAll() {
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
